package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.InvestRecord;
import com.hmyd.hmyd_android.utils.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class InvestmentHistoryListAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<InvestRecord> mPlist;
	private LayoutInflater mInflater;

	public InvestmentHistoryListAdapter(Context context, List<InvestRecord> plist){
		super();
		
		this.mContext = context;
		this.mPlist = plist;
		mInflater=LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public InvestRecord getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mPlist==null || mPlist.get(position)==null){
			return convertView;
		}
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_investment_history_list_item, null);
			holder = new ViewHolder();
			holder.nick_name = (TextView)convertView.findViewById(R.id.nick_name);
			holder.time = (TextView)convertView.findViewById(R.id.time);
			holder.amount = (TextView)convertView.findViewById(R.id.amount);
			holder.unit = (TextView)convertView.findViewById(R.id.unit);
			holder.line = convertView.findViewById(R.id.line);
			
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		InvestRecord investRecord = getItem(position);
		if(investRecord!=null){
			holder.nick_name.setText(investRecord.nickname);
			holder.time.setText(Utils.getDateString(investRecord.createdTime));
			holder.amount.setText(Utils.amountConversionFormat(Double.valueOf(investRecord.amount)));
			
			if(mPlist.size()==(position+1)){
				holder.line.setVisibility(View.INVISIBLE);
			}else{
				holder.line.setVisibility(View.VISIBLE);
			}
		}
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView nick_name;
		public TextView time;
		public TextView amount;
		public TextView unit;
		public View line;
	}
	
}
